﻿using DotNetCommon;
using furion.blog.Application.Contracts;
using furion.blog.Domain.Shared.Attribute;
using furion.blog.Domain.Shared.Dto.Sms;
using furion.blog.Utils;
using Furion;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 短信接口
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "SmsApi" })]
    [Route("api/sms")]
    public class SmsApiController : IDynamicApiController
    {
        private readonly ISmsService _smsService;
        public SmsApiController(ISmsService smsService) {
            this._smsService = smsService;
        }

        [ActionAttribute(Name = "短信验证码发送接口", Desc = "发送短信验证码")]
        [HttpPost, Route("captcha/send")]
        public async Task<IActionResult> send([FromQuery(Name = "phone")]string phone) {
            if (string.IsNullOrWhiteSpace(phone) || !phone.MatchPhoneNumber()) {
                throw Oops.Oh("手机号码有误");
            }
            var tempCode = App.Configuration["Ali:Sms:CommonTemp:TempCode"];
            var tempName = App.Configuration["Ali:Sms:CommonTemp:SignName"];
            var accessKey = App.Configuration["Ali:Sms:CommonTemp:AccessKey"];
            var accessSecret = App.Configuration["Ali:Sms:CommonTemp:AccessSecret"];
            
            var tempParams = new Dictionary<string, string>() { { "code", RandomUtil.GetInt(1000,9999).ToString() } };
            var dto = new SmsSendDto {
                Phone = phone,
                TempCode = tempCode,
                TempName = tempName,
                AccessKey = accessKey ,
                AccessSecret = accessSecret,
                TempParams = tempParams ,
            };
            var ret = await this._smsService.SendCaptcha(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

    }
}
